<?php
/**
 * DBGridFilter
 *
 * @author fsqijy
 */
require_once 'Zend/View/Helper/FormText.php';
require_once 'Zend/View/Helper/FormSelect.php';
require_once 'Zend/View/Helper/FormRadio.php';

class DBGridFilter {

    private $helper_FormText;
    private $helper_FormSelect;
    private $helper_FormRadio;

    protected $_post;
    protected $_translate;
    protected $_module;
    public function  __construct($view) {
        $this->helper_FormText  = new Zend_View_Helper_FormText();
        $this->helper_FormSelect = new Zend_View_Helper_FormSelect();
        $this->helper_FormRadio = new Zend_View_Helper_FormRadio();
        $this->helper_FormText->setView($view);
        $this->helper_FormSelect->setView($view);
        $this->helper_FormRadio->setView($view);
        $this->_translate = Zend_Registry::get('Zend_Translate');
    }
    public function render($filters,$advanceFilters,$selectOptionFilter,$module,$post=array(),$useAjax = false) {
        $this->_post = $post;
        $this->_module = $module;
        $filterHtml ="";
        $filterHtml .= $this->renderHidden();
        $filterHtml .= "<div class='dbgrid_form_filter'>";
        if(!empty ($filters) || !empty($selectOptionFilter)) {
            $filterHtml .= $this->renderFiltes($filters);
            $filterHtml .= $this->renderSelectOptionFiltes($selectOptionFilter);
            //set if use ajax call
            if($useAjax){
                $filterHtml .= "<div class='dbgrid_form_filter_btn'><input type='button' name='dbgridSearch' value='" . $this->_translate->translate("goBtn")  . "' onclick='javascript:DBGrid.searchAjaxDbGrid(1);' /></div>";
            }else{
                $filterHtml .= "<div class='dbgrid_form_filter_btn'><input type='button' name='dbgridSearch' value='" . $this->_translate->translate("goBtn")  . "' onclick='javascript:DBGrid.search();' /></div>";
            }
            
            if(!empty ($advanceFilters)) {
                $filterHtml .= "<div class='dbgrid_form_advance_text' id = 'dbgrid_form_advance_text' onclick='javascript:DBGrid.showOrHideAdvanceForm();' >". $this->_translate->translate("advancedSearch") ."</div>";
            }
        }
        $filterHtml .= "</div>";
        $filterHtml .= "<div class='clear'></div>";
        //advance search
        if(!empty ($advanceFilters)) {
            $filterHtml .= '<div class="dbgrid_form_filter" id="dbgrid_advance_filter" style="display:none;">';
            $filterHtml .= $this->renderAdvanceFilters($advanceFilters);
            $filterHtml .= "</div>";
            $filterHtml .= "<div class='clear'></div>";
        }
        return $filterHtml;
    }

    public function renderAdvanceFilters($advanceFilters) {
        $filterHtml = "";
        //$filterHtml .= "<div style='display:none' id='dbgrid_advance_filter_hide'>";
        $filterHtml .= $this->renderFiltes($advanceFilters);
        $filterHtml .= '<div class="dbgrid_form_filter_btn">';
        $filterHtml .= '<input type="button" onclick="javascript:DBGrid.advanceSearch();" value="'. $this->_translate->translate("advSeaBtn")  .'" name="dbgridSearch"/>';
        $filterHtml .= '</div>';
        //$filterHtml .= "</div>";
        $filterHtml .= "<script type='text/javascript'>";
        $filterHtml .= " $(function() {
                             DBGrid.setAdvanceSearchStatus();
                          })";
        $filterHtml .= "</script>";
        return $filterHtml;
    }

    protected function renderFiltes($filters) {
        $filterHtml = "";
        foreach ($filters as $filter) {
            $filterType = $filter['type'];
            $filterName = $filter['name'];
            $filterValue = isset($this->_post[$filterName]) ? $this->_post[$filterName] : "";
            switch ($filterType) {
            //generate text
                case "text":
                    $filterHtml .= $this->renderText($filter,$filterValue);
                    break;
                //generate select
                case "select":
                    $filterHtml .= $this->renderSelect($filter,$filterValue);
                    break;
                //generate radio
                case "radio":
                    $filterHtml .= $this->renderRadio($filter,$filterValue);
                    break;
                //generate date
                case "date":
                    $filterHtml .= $this->renderDate($filter);
                    break;
                case "between":
                    $filterHtml .= $this->renderBetween($filter);
                    break;
            }
        }
        return $filterHtml;
    }

    protected function renderSelectOptionFiltes($selectOptionFilter) {
        $filterHtml = "";
        if(!empty($selectOptionFilter)) {
            $selectOptionFilterCol = isset($this->_post['selectOptionFilter']) ? $this->_post['selectOptionFilter'] : "" ;
            $selectOptionFilterValue = isset($this->_post['selectOptionFilterValue']) ? trim($this->_post['selectOptionFilterValue']) : "";
            $filterHtml .= "<div class='dbgrid_form_filter_text'>";
            $filterHtml .= "<div class='dbgrid_form_filter_label'>";
            $filterHtml .= "<select name='selectOptionFilter'>";
            foreach($selectOptionFilter as $filter) {
                if($selectOptionFilterCol == $filter["name"]) {
                    $filterHtml .= "<option value='" . $filter["name"] . "' selected = 'selected'>";
                }else {
                    $filterHtml .= "<option value='" . $filter["name"] . "'>";
                }
                $filterHtml .= $filter["label"];
                $filterHtml .= "</option>";
            }
            $filterHtml .= "</select>";
            $filterHtml .= "</div>";
            $filterHtml .= $this->helper_FormText->formText("selectOptionFilterValue",$selectOptionFilterValue);
            $filterHtml .= "</div>";
        }
        return $filterHtml;
    }

    protected function renderText($filter,$filterValue = "") {
        $html = "";
        $html .= "<div class='dbgrid_form_filter_text'>";
        $html .= "<div class='dbgrid_form_filter_label'>";
        $html .= "<label>" . $filter['label'] ."</label>";
        $html .= "</div>";
        $html .= $this->helper_FormText->formText($filter['name'],$filterValue,$filter['attr']);
        $html .= "</div>";
        return $html;
    }

    protected function renderSelect($filter,$filterValue = "") {
        $options = array();
        $options[""] = $this->_translate->translate("selectLabel");
        foreach($filter['options'] as $key => $option) {
            $options[$key] = $option;
        }
        $html = "";
        $html .= "<div class='dbgrid_form_filter_text'>";
        $html .= "<div class='dbgrid_form_filter_label'>";
        $html .= "<label>" . $filter['label'] ."</label>";
        $html .= "</div>";
        $html .= $this->helper_FormSelect->formSelect($filter['name'],$filterValue,$filter['attr'],$options);
        $html .= "</div>";
        return $html;
    }

    protected function renderRadio($filter,$filterValue = "") {
        $html = "";
        $html .= "<div class='dbgrid_form_filter_text'>";
        $html .= "<div class='dbgrid_form_filter_label'>";
        $html .= "<label>" . $filter['label'] ."</label>";
        $html .= "</div>";
        $html .= $this->helper_FormRadio->formRadio($filter['name'],$filterValue,$filter['attr'],$filter['options'],"");
        $html .= "</div>";
        return $html;
    }

    protected function renderDate($filter) {
    //init calendar
        $script = "";
        $script .= "<script type='text/javascript'>";
        $script .= "	$(function() {
		                   $('#".$filter['name']."-1').datepicker({
			                   changeMonth: true,
			                   changeYear: true,
                               dateFormat: 'yy-mm-dd'
		                   });
	                   });";
        if($filter['operator'] == DBGridConstant::OPERATOR_BETWEEN) {
            $script .= "	$(function() {
                               $('#".$filter['name']."-2').datepicker({
                                   changeMonth: true,
                                   changeYear: true,
                                   dateFormat: 'yy-mm-dd'
                               });
                           });";
        }
        $script .= "</script>";
        //init html
        $html = "";
        $html .= "<div class='dbgrid_form_filter_text'>";
        $html .= "<div class='dbgrid_form_filter_label'>";
        $html .= "<label>" . $filter['label'] ."</label>";
        $html .= "</div>";
        $filterValue1 = isset($this->_post[$filter['name']."-1"]) ? $this->_post[$filter['name']."-1"] : "";
        $filterValue2 = isset($this->_post[$filter['name']."-2"]) ? $this->_post[$filter['name']."-2"] : "";
        if($filter['operator'] == DBGridConstant::OPERATOR_BETWEEN) {
            $html .= "<input type='text' name='" . $filter['name'] . "-1' id='". $filter['name'] ."-1' size='8' value='$filterValue1' />" ;
            $html .= "<span>&nbsp;-&nbsp;</span>";
            $html .= "<input type='text' name='" . $filter['name'] . "-2' id='". $filter['name'] ."-2' size='8' value='$filterValue2' style='float:none;' />" ;
        }else {
            $html .= "<input type='text' name='" . $filter['name'] . "-1' id='". $filter['name'] ."-1' size='8' value='$filterValue1' />" ;
        }
        $html .= "</div>";
        return $script . $html;
    }

    protected function renderHidden() {
        $sortCol = "";
        $sortOrder = "";
        if(!empty($this->_post['sortCol']) && !empty($this->_post['sortOrder'])) {
            $sortCol = $this->_post['sortCol'];
            $sortOrder = $this->_post['sortOrder'];
        }
        $pageOptionValue = isset($this->_post['dbgridPageOption']) ? intval($this->_post['dbgridPageOption']) : DBGridConstant::DEFAULT_ITEM_COUNT_PER_PAGE;
        $advanceSearchStatus = isset($this->_post['advanceSearchStatus']) ? $this->_post['advanceSearchStatus'] : "0" ;
        $module = $this->_module;
        $htmlHidden = "";
        $htmlHidden .= "<div class='dbgrid_hidden'>";
        $htmlHidden .= "<input type='hidden' name='dbgridModule' id='dbgridModule' value='$module' />";
        $htmlHidden .= "<input type='hidden' name='currentPageNumber' id='currentPageNumber' value='1' />";
        $htmlHidden .= "<input type='hidden' name='selectIds' id='selectIds' value='' />";
        $htmlHidden .= "<input type='hidden' name='sortCol' id='sortCol' value='$sortCol' />";
        $htmlHidden .= "<input type='hidden' name='sortOrder' id='sortOrder' value='$sortOrder' />";
        $htmlHidden .= "<input type='hidden' name='dbgridPageOption' id='dbgridPageOption' value='$pageOptionValue' />";
        $htmlHidden .= "<input type='hidden' name='dbgridAction' id='dbgridAction' value='' />";
        $htmlHidden .= "<input type='hidden' value='$advanceSearchStatus' name='advanceSearchStatus' id='advanceSearchStatus'>";
        $htmlHidden .= "<input type='hidden' value='0' name='useAjax' id='useAjax'>";
        $htmlHidden .= "</div>";
        return $htmlHidden;
    }
    
    protected function renderBetween($filter){
        $html = "";
        $html .= "<div class='dbgrid_form_filter_text'>";
        $html .= "<div class='dbgrid_form_filter_label'>";
        $html .= "<label>" . $filter['label'] ."</label>";
        $html .= "</div>";
        $filterValue1 = isset($this->_post[$filter['name']."-1"]) ? $this->_post[$filter['name']."-1"] : "";
        $filterValue2 = isset($this->_post[$filter['name']."-2"]) ? $this->_post[$filter['name']."-2"] : "";
       
        $html .= "<input type='text' name='" . $filter['name'] . "-1' id='". $filter['name'] ."-1' size='8' value='$filterValue1' />" ;
        $html .= "<span>&nbsp;-&nbsp;</span>";
        $html .= "<input type='text' name='" . $filter['name'] . "-2' id='". $filter['name'] ."-2' size='8' value='$filterValue2' style='float:none;' />" ;
       
        $html .= "</div>";
        return $html;
    }
}